class Logger {
  //实例属性
  final String name;
  bool mute = false;

  // _cache is library-private, thanks to
  // the _ in front of its name.
  static final Map<String, Logger> _cache = <String, Logger>{}; //类属性

  factory Logger(String name) {
    //使用factory关键字声明工厂构造函数，
    if (_cache.containsKey(name)) {
      return _cache[name]; //返回缓存已经创建实例
    } else {
      final logger = Logger._internal(
          name); //缓存中找不到对应的name logger，调用_internal命名构造函数创建一个新的Logger实例
      _cache[name] = logger; //并把这个实例加入缓存中
      return logger; //注意: 最后返回这个新创建的实例
    }
  }

  Logger._internal(this.name); //定义一个命名私有的构造函数_internal

  void log(String msg) {
    //实例方法
    if (!mute) print(msg);
  }
}

main(List<String> args) {
  Logger a = Logger('first');
  a.log('msg');
  Logger b = Logger('first');
  print(a==b);
}
